package com.hxk.juc.thread.volatileLearn;

/**
 * 双重检锁式单例模式
 * 在多线程情况下会出现【指令重排】问题，可以使用volatile来处理这个问题
 */
public class VolatileSingleCase {
    private volatile static VolatileSingleCase instance = null;

    private VolatileSingleCase(){
        System.out.println(Thread.currentThread().getName()+"\t SingleCase3的私有构造方法");
    }

    public static VolatileSingleCase getInstance(){
        if (instance == null) {
            synchronized (VolatileSingleCase.class){
                if (instance == null){
                    instance = new VolatileSingleCase();
                }
            }
        }
        return instance;
    }

    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            new Thread(()->{
                VolatileSingleCase.getInstance();
            },String.valueOf(i)).start();
        }
    }

}
